(* This tool creates simple header files for available themes and highlighters 
   found in the ace install *)
program createlist;
{$mode objfpc}{$H+}
uses Classes, SysUtils, StrUtils;  

procedure GetColoursFromTheme(filename: String; var bgColour: String; var fColour: String);
var
  f: TStrings;
  tname: String;
  vS, vE: Integer;  
  i: integer;
begin
  f := TStringList.Create;
  f.LoadFromFile(filename);
  tname := Copy(ExtractFileName(filename),Pos('-',ExtractFileName(filename))+1,Pos('.',ExtractFileName(filename))-Pos('-',ExtractFileName(filename))-1);
  tname := AnsiReplaceStr(tname,'_','-');
  writeln(tname);
  vS := 0;
  vE := 0;
  repeat
    inc(vS);
    if vS > (f.Count-1) then break;
  until AnsiStartsStr('.ace-'+tname+' {',f[vS]) = true;
  if vS > f.Count then vS := 0;  
  vE := vS;
  repeat
    inc(vE);
    if vE > f.Count then break;    
  until AnsiStartsStr('}\',f[vE]) = true;
  if vE > f.Count then vE := f.Count -1;    
  for i := vS to vE do
  begin
   //writeln(f[i]);
   if AnsiStartsStr('background',f[i]) then
   begin
     bgColour := Copy(f[i],Pos(':',f[i])+1,Pos(';',f[i])-Pos(':',f[i])-1);
     bgColour := TrimSet(bgColour,[' ',';','\',#13,#10]);
     //writeln(f[i]);
     writeln('bg: ',bgColour);     
   end;
   if AnsiStartsStr('color:',f[i]) then
   begin
     fColour := Copy(f[i],Pos(':',f[i])+1,Pos('\',f[i])-Pos(':',f[i])-1);   
     fColour := TrimSet(fColour,[' ',';','\',#13,#10]);     
     //writeln(f[i]);   
       
     writeln('fg: ',fColour);     
   end;
  end;
  f.Free;
end;

function GetFileExtensionsForHighlighter(hName: String): String;
var
  f: TStrings;
  vS, vE: Integer;
  i: Integer;
begin
  Result := '';
  f := TStringList.Create;
  f.LoadFromFile('src-noconflict/ext-modelist.js');
  vS := 0;
  vE := 0;
  repeat
    inc(vS);
  until AnsiStartsStr('var supportedModes = {',f[vS]) = true;
  vE := vS;
  repeat
    inc(vE);
  until AnsiStartsStr('};',f[vE]) = true;
  for i := vS+1 to vE-1 do 
  begin
    if AnsiContainsStr(Lowercase(f[i]),Lowercase(hName)) then
    begin
      Result := Copy(f[i],Pos('[',f[i])+2,Pos(']',f[i])-Pos('[',f[i])-3);
    end;
  end;  
  f.Free;
end;

procedure main;
var
  s: TSearchRec;
  i: integer;
  tmp: String;
  oH: TStrings;
  oT: TStrings;
  bg, fg: String;
begin
  oH := TStringList.Create;
  oH.Add('#ifndef HIGHLIGHTERS_H');
  oH.Add('#define HIGHLIGHTERS_H');
  oH.Add('');
  oH.Add('struct THighlighter {');
  oH.Add('    QString displayName;');
  oH.Add('    QStringList fileExt;');
  oH.Add('};  ');      
  oH.Add('');
  oH.Add('class QAceHighlighters : public QObject {');  
  oH.Add('    Q_OBJECT');
  oH.Add('');
  oH.Add('  public:'); 
  oH.Add('    QString GetHighlighter(QString s);');
  oH.Add('    void InitHighlighters();');  
  oH.Add('  private:');   
  oH.Add('    QList<THighlighter> highlighters;');  
  oH.Add('};');
  oH.Add('');  
  oH.Add('void QAceHighlighters::InitHighlighters() {');
  oH.Add('  THighlighter h;');
  

  oT := TStringList.Create;
  oT.Add('#ifndef THEMES_H');
  oT.Add('#define THEMES_H');
  oT.Add('');
  oT.Add('#include <QList>');
  oT.Add('');
  oT.Add('struct TTheme {');
  oT.Add('    QString displayName;');
  oT.Add('    QString bgColour;');
  oT.Add('    QString fgColour;');  
  oT.Add('};  ');      
  oT.Add('');   
  oT.Add('TTheme theme;');
  oT.Add('QList<TTheme> themes;');
  oT.Add('');     
  
  if FindFirst('src-noconflict/*',faAnyFile, s) = 0 then
  begin
    repeat
      if (s.Attr and faDirectory) <> faDirectory then
      begin
        if Lowercase(ExtractFileExt(s.Name)) = '.js' then
        begin
          // Highlighter
          if AnsiStartsStr('mode-',s.Name) then
          begin
            tmp := Copy(s.Name,Pos('-',s.Name)+1,Pos('.',s.Name)-Pos('-',s.Name)-1);
            if GetFileExtensionsForHighlighter(tmp) <> '' then 
            begin
              oH.Add('  h.displayName = "'+tmp+'";');
              oH.Add('  h.fileExt = "'+GetFileExtensionsForHighlighter(tmp)+'";');
              oH.Add('  highlighters.append(h);');
            end;
          end;
          // Theme
          if AnsiStartsStr('theme-',s.Name) then
          begin
            bg := '';
            fg := '';
            tmp := Copy(s.Name,Pos('-',s.Name)+1,Pos('.',s.Name)-Pos('-',s.Name)-1);
            GetColoursFromTheme('src-noconflict/' + s.Name,bg,fg);
            oT.Add('theme.displayName = "' + tmp + '";');
            oT.Add('theme.bgColour = "'+ bg + '";');
            oT.Add('theme.fgColour = ' + fg + '";');
            oT.Add('themes.append(theme);');
            oT.Add('');               
          end;
        end;
      end;
    until FindNext(s) <> 0;
  end;
  FindClose(s);
  oH.Add('}');
//  oH.SaveToFile('highlighters.h');
  oT.SaveToFile('themes.h');
end;

begin
  main;
end.
